//
//  TXPLandPageViewController.m
//  StayAwhile
//
//  Created by dllo on 16/1/27.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "TXPLandPageViewController.h"
#import "TXPLoginPageViewController.h"

@interface TXPLandPageViewController ()

@property (nonatomic, retain) UITextField *userTextField;
@property (nonatomic, retain) UITextField *keyTextField;
@property (nonatomic, retain) UIButton *loadButton;
@property (nonatomic, retain) UIButton *weiboBtn;
@property (nonatomic, retain) UIButton *qqBtn;
@property (nonatomic, retain) UIButton *weixinBtn;

@end

@implementation TXPLandPageViewController
- (void)viewWillAppear:(BOOL)animated{
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = RGBA(245, 245, 245, 1);
    
    UIButton *returnButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [returnButton setImage:[UIImage imageNamed:@"iconfont-fanhui-6.png"] forState:UIControlStateNormal];
    [self.view addSubview:returnButton];
    [returnButton addTarget:self action:@selector(returnBtn:) forControlEvents:UIControlEventTouchUpInside];
    [returnButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo (self.view.mas_left).offset(20);
        make.top.mas_equalTo (self.view.mas_top).offset(20);
        make.size.mas_equalTo(CGSizeMake(45, 40));
        
    }];
   
    UIButton *loginButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [loginButton setTitle:@"注册" forState:UIControlStateNormal];
    loginButton.titleLabel.font = [UIFont systemFontOfSize:20];
    [self.view addSubview:loginButton];
    [loginButton addTarget:self action:@selector(clickLogin:) forControlEvents:UIControlEventTouchUpInside];
    [loginButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [loginButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo (self.view.mas_right).offset(- 60);
        make.top.mas_equalTo (self.view.mas_top).offset(20);
        make.size.mas_equalTo(CGSizeMake(40, 40));
        
    }];

    UILabel *userLabel = [[UILabel alloc] init];
    userLabel.text = @"昵称:";
    [self.view addSubview:userLabel];
    [userLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo (self.view.mas_left).offset(60);
        make.top.mas_equalTo (self.view.mas_top).offset(120);
        make.size.mas_equalTo(CGSizeMake(70, 40));
        
    }];
    
    UIView *lineView = [[UIView alloc] init];
    [self.view addSubview:lineView];
    lineView.backgroundColor = [UIColor lightGrayColor];
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo (userLabel.mas_bottom).offset(5);
        make.left.mas_equalTo (self.view.mas_left).offset(60);
        
        make.size.mas_equalTo (CGSizeMake(375 - 120, 1));
        
    }];

    self.userTextField = [[UITextField alloc] init];
    [[UITextField appearance] setTintColor:[UIColor blackColor]];
    [self.view addSubview:self.userTextField];
    self.userTextField.borderStyle = UITextBorderStyleNone;
    [self.userTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo (userLabel.mas_right).offset(0);
        make.top.mas_equalTo (self.view.mas_top).offset(120);
        make.size.mas_equalTo(CGSizeMake(375 - 120, 40));
        
    }];

    UILabel *keyLabel = [[UILabel alloc] init];
    keyLabel.text = @"密码:";
    [self.view addSubview:keyLabel];
    [keyLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo (self.view.mas_left).offset(60);
        make.top.mas_equalTo (lineView.mas_bottom).offset(40);
        make.size.mas_equalTo(CGSizeMake(70, 40));
        
    }];
    
    self.keyTextField = [[UITextField alloc] init];
    [[UITextField appearance] setTintColor:[UIColor blackColor]];
    self.keyTextField.secureTextEntry = YES;
    [self.view addSubview:self.keyTextField];
    self.keyTextField.borderStyle = UITextBorderStyleNone;
    [self.keyTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo (keyLabel.mas_right).offset(0);
        make.top.mas_equalTo (lineView.mas_bottom).offset(40);
        make.size.mas_equalTo(CGSizeMake(WIDTH - 120, 40));
        
    }];

    UIView *twolineView = [[UIView alloc] init];
    [self.view addSubview:twolineView];
    twolineView.backgroundColor = [UIColor lightGrayColor];
    [twolineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo (keyLabel.mas_bottom).offset(5);
        make.left.mas_equalTo (self.view.mas_left).offset(60);
        
        make.size.mas_equalTo (CGSizeMake(WIDTH - 120, 1));
        
    }];
    
    self.loadButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.loadButton.backgroundColor = RGBA(119, 183, 69, 1);
    [self.loadButton setTitle:@"登录" forState:UIControlStateNormal];
    self.loadButton.tintColor = RGBA(119, 183, 69, 1);
    [self.loadButton addTarget:self action:@selector(clickLoadButton:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.loadButton];
    [self.loadButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo (twolineView.mas_bottom).offset(60);
        make.left.mas_equalTo (self.view.mas_left).offset(60);
        
        make.size.mas_equalTo (CGSizeMake(WIDTH - 120, 50));
        
    }];
    
    UIView *threelineView = [[UIView alloc] init];
    [self.view addSubview:threelineView];
    threelineView.backgroundColor = [UIColor lightGrayColor];
    [threelineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo (self.loadButton.mas_bottom).offset(70);
        make.left.mas_equalTo (self.view.mas_left).offset(70);
        
        make.size.mas_equalTo (CGSizeMake((WIDTH - 240) / 2, 1));
        
    }];
    
    UILabel *thirdTextLabel = [[UILabel alloc] init];
    [self.view addSubview:thirdTextLabel];
    thirdTextLabel.text = @"合作网站登录";
    thirdTextLabel.font = [UIFont boldSystemFontOfSize:19];
    thirdTextLabel.textColor = RGBA(108, 108, 108, 1);
    [thirdTextLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo (self.loadButton.mas_bottom).offset(50);
        make.left.mas_equalTo (threelineView.mas_right).offset(0);
        
        make.size.mas_equalTo (CGSizeMake(115, 40));
        
    }];

    UIView *fourlineView = [[UIView alloc] init];
    [self.view addSubview:fourlineView];
    fourlineView.backgroundColor = [UIColor lightGrayColor];
    [fourlineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo (self.loadButton.mas_bottom).offset(70);
        make.left.mas_equalTo (thirdTextLabel.mas_right).offset(0);
        
        make.size.mas_equalTo (CGSizeMake((WIDTH - 230) / 2, 1));
        
    }];

    self.weiboBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.weiboBtn setImage:[UIImage imageNamed:@"iconfont-weibo.png"] forState:UIControlStateNormal];
    [self.view addSubview:self.weiboBtn];
    [self.weiboBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo (threelineView.mas_bottom).offset(50);
        make.left.mas_equalTo (self.view.mas_left).offset(80);
        make.size.mas_equalTo (CGSizeMake(50, 50));
        
    }];

    self.qqBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.qqBtn setImage:[UIImage imageNamed:@"iconfont-qq-copy.png"] forState:UIControlStateNormal];
    [self.view addSubview:self.qqBtn];
    [self.qqBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo (threelineView.mas_bottom).offset(50);
        make.left.mas_equalTo (self.view.mas_left).offset(170);
        make.size.mas_equalTo (CGSizeMake(50, 50));
        
    }];

    self.weixinBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.weixinBtn setImage:[UIImage imageNamed:@"iconfont-weixin.png"] forState:UIControlStateNormal];
    [self.view addSubview:self.weixinBtn];
    [self.weixinBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo (threelineView.mas_bottom).offset(50);
        make.left.mas_equalTo (self.view.mas_left).offset(260);
        make.size.mas_equalTo (CGSizeMake(50, 50));
        
    }];


}

- (void)clickLogin:(UIButton *)btn{
    TXPLoginPageViewController *loginPageVC = [[TXPLoginPageViewController alloc] init];
    [self.navigationController pushViewController:loginPageVC animated:YES];
    
}
- (void)clickLoadButton:(UIButton *)btn{
    [BmobUser loginWithUsernameInBackground:self.userTextField.text password:self.keyTextField.text block:^(BmobUser *user, NSError *error) {
        if (user) {
            NSLog(@"%@", user);
            [self.navigationController popToRootViewControllerAnimated:YES];
            
            
        } else {
            NSLog(@"%@", error);
            MBProgressHUD *promptHUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
            promptHUD.mode = MBProgressHUDModeText;
            promptHUD.labelText = @"用户不存在";
            promptHUD.labelColor = [UIColor whiteColor];
            [promptHUD hide:YES afterDelay:0.5];

        }
    }];

}
- (void)returnBtn:(UIButton *)btn{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
